home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / comm / term / term34Source.lha / termPickScreen.c < prev    next >
C/C++ Source or Header  |  1993-07-16  |  7KB  |  317 lines

  1. /*
  2. **    termPickScreen.c
  3. **
  4. **    Simplified public screen selection routine
  5. **
  6. **    Copyright © 1990-1993 by Olaf `Olsen' Barthel & MXM
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12. enum    {    GAD_LIST,GAD_USE,GAD_CANCEL };
  13.  
  14.     /* CreateAllGadgets():
  15.      *
  16.      *    Get them gadgets goin'.
  17.      */
  18.  
  19. STATIC struct Gadget *
  20. CreateAllGadgets(struct Gadget **GadgetArray,struct Gadget **GadgetList,struct List *List,STRPTR Current,LONG *Index)
  21. {
  22.     struct Gadget        *Gadget;
  23.     struct NewGadget     NewGadget;
  24.     UWORD             Counter = 0;
  25.     struct Node        *Node,
  26.                 *Next;
  27.     BYTE             GotIt = FALSE;
  28.  
  29.     *Index = 0;
  30.  
  31.     Node = List -> lh_Head;
  32.  
  33.     while(!GotIt && (Next = Node -> ln_Succ))
  34.     {
  35.         if(!strcmp(Node -> ln_Name,Current))
  36.             GotIt = TRUE;
  37.         else
  38.             (*Index)++;
  39.  
  40.         Node = Next;
  41.     }
  42.  
  43.     if(!GotIt)
  44.         *Index = ~0;
  45.  
  46.     SZ_SizeSetup(Window -> WScreen,&UserFont,TRUE);
  47.  
  48.     memset(&NewGadget,0,sizeof(struct NewGadget));
  49.  
  50.     if(Gadget = CreateContext(GadgetList))
  51.     {
  52.         WORD ButtonWidth,ListWidth;
  53.  
  54.         SZ_ResetMaxWidth();
  55.  
  56.         SZ_UpdateMaxWidth(BUTTON_KIND,LocaleString(MSG_GLOBAL_USE_GAD),0,NULL);
  57.         SZ_UpdateMaxWidth(BUTTON_KIND,LocaleString(MSG_GLOBAL_CANCEL_GAD),0,NULL);
  58.  
  59.         ButtonWidth = SZ_ResetMaxWidth();
  60.  
  61.         SZ_UpdateMaxWidth(LISTVIEW_KIND,NULL,40,NULL);
  62.  
  63.         ListWidth = SZ_ResetMaxWidth();
  64.  
  65.         if(ListWidth < 2 * ButtonWidth + InterWidth)
  66.             ListWidth = 2 * ButtonWidth + InterWidth;
  67.         else
  68.             ButtonWidth = (ListWidth - InterWidth) / 2;
  69.  
  70.         SZ_SetWidth(ListWidth);
  71.  
  72.         NewGadget . ng_GadgetText    = LocaleString(MSG_TERMPICKSCREEN_SCREEN_LIST_GAD);
  73.         NewGadget . ng_GadgetID        = Counter;
  74.         NewGadget . ng_Flags        = PLACETEXT_ABOVE;
  75.  
  76.         GadgetArray[Counter++] = Gadget = CreateGadget(LISTVIEW_KIND,Gadget,&NewGadget,
  77.             SZ_Adjust,        TRUE,
  78.             SZ_AutoWidth,        TRUE,
  79.             SZ_Lines,        10,
  80.             SZ_InterHeight,        0,
  81.  
  82.             GTLV_Labels,        List,
  83.             GTLV_ShowSelected,    NULL,
  84.             GTLV_Selected,        *Index,
  85.         TAG_DONE);
  86.  
  87.         SZ_SetWidth(ButtonWidth);
  88.  
  89.         NewGadget . ng_GadgetText    = LocaleString(MSG_GLOBAL_USE_GAD);
  90.         NewGadget . ng_GadgetID        = Counter;
  91.         NewGadget . ng_Flags        = 0;
  92.  
  93.         GadgetArray[Counter++] = Gadget = CreateGadget(BUTTON_KIND,Gadget,&NewGadget,
  94.             SZ_Adjust,    TRUE,
  95.             SZ_AutoWidth,    TRUE,
  96.             SZ_AlignExtra,    TRUE,
  97.             SZ_AlignLeft,    TRUE,
  98.             SZ_AlignBottom,    TRUE,
  99.             SZ_GroupCount,    2,
  100.         TAG_DONE);
  101.  
  102.         NewGadget . ng_GadgetText    = LocaleString(MSG_GLOBAL_CANCEL_GAD);
  103.         NewGadget . ng_GadgetID        = Counter;
  104.  
  105.         GadgetArray[Counter++] = Gadget = CreateGadget(BUTTON_KIND,Gadget,&NewGadget,
  106.             SZ_Adjust,    TRUE,
  107.             SZ_AutoWidth,    TRUE,
  108.             SZ_GroupNext,    TRUE,
  109.         TAG_DONE);
  110.     }
  111.  
  112.     return(Gadget);
  113. }
  114.  
  115.     /* BuildScreenList():
  116.      *
  117.      *    Build a private copy of the public screen list.
  118.      */
  119.  
  120. STATIC struct List *
  121. BuildScreenList(VOID)
  122. {
  123.     struct List *List,*PubScreenList;
  124.  
  125.         /* Get the list body. */
  126.  
  127.     if(List = (struct List *)AllocVec(sizeof(struct List),MEMF_ANY))
  128.     {
  129.         NewList(List);
  130.  
  131.             /* Get access to the public screen list. */
  132.  
  133.         if(PubScreenList = LockPubScreenList())
  134.         {
  135.             struct Node *Next,*Node;
  136.  
  137.             Node = PubScreenList -> lh_Head;
  138.  
  139.                 /* Scan the list. */
  140.  
  141.             while(Next = Node -> ln_Succ)
  142.             {
  143.                     /* Don't include the current `term' public
  144.                      * screen name in it.
  145.                      */
  146.  
  147.                 if(strcmp(Node -> ln_Name,TermIDString))
  148.                 {
  149.                     struct Node *New = CreateNode(Node -> ln_Name);
  150.  
  151.                         /* Got a new node? */
  152.  
  153.                     if(New)
  154.                         AddTail(List,New);
  155.                 }
  156.  
  157.                 Node = Next;
  158.             }
  159.  
  160.             UnlockPubScreenList();
  161.         }
  162.  
  163.             /* In case the list happens to remain empty,
  164.              * include the Workbench screen in it.
  165.              */
  166.  
  167.         if(!List -> lh_Head -> ln_Succ)
  168.         {
  169.             struct Node *New = CreateNode("Workbench");
  170.  
  171.             if(New)
  172.                 AddTail(List,New);
  173.             else
  174.             {
  175.                 FreeVec(List);
  176.  
  177.                 List = NULL;
  178.             }
  179.         }
  180.     }
  181.  
  182.     return(List);
  183. }
  184.  
  185.     /* PickScreen(STRPTR Name):
  186.      *
  187.      *    Your nice public screen selection routine.
  188.      */
  189.  
  190. BYTE
  191. PickScreen(STRPTR Name)
  192. {
  193.     struct List    *ScreenList;
  194.     BYTE         Result = FALSE;
  195.  
  196.     if(ScreenList = BuildScreenList())
  197.     {
  198.         struct Window    *PanelWindow;
  199.         struct Gadget    *GadgetList = NULL;
  200.         struct Gadget    *GadgetArray[GAD_CANCEL + 1];
  201.         LONG         Index;
  202.  
  203.         if(CreateAllGadgets(GadgetArray,&GadgetList,ScreenList,Name,&Index))
  204.         {
  205.             if(PanelWindow = OpenWindowTags(NULL,
  206.                 WA_Left,        GetScreenLeft(Window) + (GetScreenWidth(Window) - SZ_GetWindowWidth()) / 2,
  207.                 WA_Top,            GetScreenTop(Window) + (GetScreenHeight(Window) - SZ_GetWindowHeight())    / 2,
  208.                 WA_Width,        SZ_GetWindowWidth(),
  209.                 WA_Height,        SZ_GetWindowHeight(),
  210.  
  211.                 WA_Activate,        TRUE,
  212.                 WA_DragBar,        TRUE,
  213.                 WA_DepthGadget,        TRUE,
  214.                 WA_RMBTrap,        TRUE,
  215.                 WA_CloseGadget,        TRUE,
  216.                 WA_CustomScreen,    Window -> WScreen,
  217.                 WA_NoCareRefresh,    TRUE,
  218.  
  219.                 WA_IDCMP,        IDCMP_CLOSEWINDOW | IDCMP_VANILLAKEY | IDCMP_ACTIVEWINDOW | LISTVIEWIDCMP | BUTTONIDCMP,
  220.  
  221.                 WA_Title,        LocaleString(MSG_TERMPICKSCREEN_SCREENS_TXT),
  222.             TAG_DONE))
  223.             {
  224.                 struct IntuiMessage    *Massage;
  225.                 ULONG             IClass,Code;
  226.                 struct Gadget        *Gadget;
  227.                 BYTE             Terminated = FALSE;
  228.  
  229.                 PushWindow(PanelWindow);
  230.  
  231.                 AddGList(PanelWindow,GadgetList,(UWORD)-1,(UWORD)-1,NULL);
  232.                 RefreshGList(GadgetList,PanelWindow,NULL,(UWORD)-1);
  233.                 GT_RefreshWindow(PanelWindow,NULL);
  234.  
  235.                 if(Index != ~0)
  236.                 {
  237.                     GT_SetGadgetAttrs(GadgetArray[GAD_LIST],PanelWindow,NULL,
  238.                         GTLV_Top,        Index,
  239.                         GTLV_MakeVisible,    Index,
  240.                     TAG_DONE);
  241.                 }
  242.  
  243.                 ClrSignal(SIG_BREAK);
  244.  
  245.                 while(!Terminated)
  246.                 {
  247.                     if(Wait(PORTMASK(PanelWindow -> UserPort) | SIG_BREAK) & SIG_BREAK)
  248.                         break;
  249.  
  250.                     while(!Terminated && (Massage = (struct IntuiMessage *)GT_GetIMsg(PanelWindow -> UserPort)))
  251.                     {
  252.                         IClass    = Massage -> Class;
  253.                         Code    = Massage -> Code;
  254.                         Gadget    = (struct Gadget *)Massage -> IAddress;
  255.  
  256.                         GT_ReplyIMsg(Massage);
  257.  
  258.                         KeySelect(GadgetArray,GAD_CANCEL,Code,PanelWindow,&Gadget,&IClass,&Code);
  259.  
  260.                         if(IClass == IDCMP_CLOSEWINDOW)
  261.                             Terminated = TRUE;
  262.  
  263.                         if(IClass == IDCMP_GADGETUP)
  264.                         {
  265.                             switch(Gadget -> GadgetID)
  266.                             {
  267.                                 case GAD_USE:
  268.  
  269.                                     if(Index != ~0)
  270.                                     {
  271.                                         struct Node *Node;
  272.  
  273.                                         if(Node = GetListNode(Index,ScreenList))
  274.                                         {
  275.                                             strcpy(Name,Node -> ln_Name);
  276.  
  277.                                             Result = TRUE;
  278.                                         }
  279.                                     }
  280.  
  281.                                     Terminated = TRUE;
  282.                                     break;
  283.  
  284.                                 case GAD_CANCEL:
  285.  
  286.                                     Terminated = TRUE;
  287.                                     break;
  288.  
  289.                                 case GAD_LIST:
  290.  
  291.                                     Index = Code;
  292.                                     break;
  293.                             }
  294.                         }
  295.                     }
  296.                 }
  297.  
  298.                 RemoveGList(PanelWindow,GadgetList,(UWORD)-1);
  299.  
  300.                 PopWindow();
  301.  
  302.                 CloseWindow(PanelWindow);
  303.             }
  304.         }
  305.  
  306.         FreeGadgets(GadgetList);
  307.  
  308.         FreeList(ScreenList);
  309.  
  310.         FreeVec(ScreenList);
  311.     }
  312.     else
  313.         DisplayBeep(Window -> WScreen);
  314.  
  315.     return(Result);
  316. }
  317.